{% extends "admin_base.html" %}
{% block title %}{{ post_type|capitalize }}s {% if is_draft %} -- draft {% endif %}{% endblock %}
{% block main %}
<div class="row">
    <!-- <div class="col-md-10"> -->
    <!-- <h1>New Post</h1> -->
    
    <table class="table table-striped">
        <thead>
            <tr>
                <th>No.</th>
                <th style="width: 15%" >Title</th>
                <th>Abstract</th>
                <th>Author</th>
                <th style="width: 10%">Publish</th>
                <th style="width: 10%">Update</th>
                <th style="width: 5%">Weight</th>
                <th style="width: 5%">Actions</th>
            </tr>
        </thead>
        <tbody>
            {% for post in posts.items %}
            <tr id="post-{{ post.id }}">
                <td>{{ loop.index }}</td>
                <td> 
                    <a href="{{ url_for('main.post_general_preview', slug=post.slug, post_type=post_type) }}?is_preview={{ is_draft }}" target="_blank">{{ post.title }}</a>                   
                </td>
                <td>{{ post.abstract }}</td>
                <td>{{ post.author.username }}</td>
                <td>{{ post.pub_time.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                <td>{{ post.update_time.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                <td>{{ post.weight }}</td>
                <td>
                    <a href="{{ url_for('blog_admin.edit_post', slug=post.slug) }}"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span> </a>
                    &nbsp
                    <a href="{{ url_for('blog_admin.edit_post', slug=post.slug) }}?is_draft={{ is_draft }}"  class="del-post"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> </a>
                </td>
            </tr>
            {% else %}
            <tr><td colspan="7" style="text-align: left">No {{ post_type }}s yet</td></tr>
            {% endfor %}      
        </tbody>
    </table>
    {% import '_pagination.html' as pagination %}
    {{ pagination.render_pagination(posts) }}
    <!-- </div> -->
    <!-- <div class="col-md-2">.col-md-2</div> -->
</div>
{% endblock %}
{% block js %}
<script type="text/javascript">
$('.del-post').on('click', function (event) {
    // var answer = confirm("Delete post?");
    // return answer;
    var url = $(this).attr('href');
    url = url + '&ajax=true';
    bootbox.confirm("Are you sure?", function(result) {
      if(result){
        // $.get(url, function(){
        //     location.reload();
        // });
        $.ajax({
            url: url,
            type: 'DELETE',
            success: function(result){
                location.reload();
            }

        })
        }
    });
    return false;
  });
</script>
{% endblock %}